<div class="accordion-group">
	<div class="accordion-heading">
		<a class="accordion-toggle" data-toggle="collapse"
			data-parent="#serveradmin" href="#restart"> Restart Services </a>
	</div>
	<div id="restart" class="accordion-body collapse">
		<div class="accordion-inner">
			<form>
				<?php echo CHtml::dropdownList('id_account', '', array($whmaccount->id_account => $whmaccount->domain), array('empty' => 'Restart Server (1 Account)')); ?><br />
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('httpd', false, array('value' => 1, 'class' => 'services')); ?>
					Apache Web Server
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('mysql', false, array('value' => 1, 'class' => 'services')); ?>
					MySQL Server
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('sshd', false, array('value' => 1, 'class' => 'services')); ?>
					SSH Daemon
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('named', false, array('value' => 1, 'class' => 'services')); ?>
					DNS Server (BIND)
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('ftpd', false, array('value' => 1, 'class' => 'services')); ?>
					Ftp Service (ProFTPd/Pure-FTPd)
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('exim', false, array('value' => 1, 'class' => 'services')); ?>
					Exim Mail
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('imap', false, array('value' => 1, 'class' => 'services')); ?>
					IMAP Server (Courier IMAP/WU IMAP)
				</label>
				
				<label class="checkbox">
					<?php echo CHtml::checkBox('cppop', false, array('value' => 1, 'class' => 'services')); ?>
					POP3 Server (Courier POP3/cppop) 
				</label>
				
<!--				<label class="checkbox">-->
					<?php echo CHtml::checkBox('accounts_check_all', false, array('value' => 1)); ?>
					<button class="btn btn-small btn-primary" id="restart_btn" type="button" data-loading-text="Restarting...">Restart Services</button>
<!--				</label>-->
				<script>
					$('#accounts_check_all').click(function(){
						$('.services').removeAttr('checked');
						if($(this).is(":checked")){
							$('.services').attr('checked', true);
						}
					});
	
					$('.services').live('click', function(){
						$('#accounts_check_all').attr('checked', true);
						$('.services').each(function(k, v){
							if(!$(v).is(":checked")){
							$('#accounts_check_all').removeAttr('checked');
								return true;
							}
						});
					});
				</script>
			</form>
		</div>
	</div>
</div>

<script>
	var restartRequest = function(){
		this.requestUrl = '';
		this.id_accounts = null;
		this.services = [];

		this.beforeRequest = function(id_account, doTimes){};
		this.requestCallback = function(id_account, serverReturn, doTimes){};
		this.afterDone = function(){};

		this.doTimes = 0;
		this.do = function(){
			this.doTimes++;
			var id_account = this.id_accounts.pop();
			if(!id_account){
				this.afterDone();
				return false;
			}
			this.beforeRequest(id_account, this.doTimes);
			var thisObj = this;
			$.ajax({
				type: 'post',
				url: thisObj.requestUrl,
//				data: $.extend( 
//					{id_account: id_account},
//					thisObj.services
//				),
				data: {
					id_account: id_account,
					services: thisObj.services
				},
				success: function(ret){
					thisObj.requestCallback(id_account, ret, thisObj.doTimes);
					thisObj.do();
					return true;
				},
				dataType: 'json'
			});
		};
	};

	$('#restart_btn').click(function(){
		var id_accounts = [];
		if($('#id_account').val() == ''){
			$('#id_account option[value!=""]').each(function(k, v){
				id_accounts.push($(v).val());
			});
		}else{
			id_accounts.push($('#id_account').val());
		}

		$('#restart_info').blink({
			'beforeCall': function(){
				$('#restart_info .content').append(
					'<p><b>Status ('+id_accounts.length+' Account)</b></p>'
				);
			},
			'callBack': function(){
				var restartRequester = new restartRequest();
				var length = id_accounts.length;
				restartRequester.id_accounts = id_accounts;
				$('.services').each(function(k, v){
					if($(v).is(':checked')){
						restartRequester.services.push($(v).attr('name'));
					}
				});
				if($.isEmptyObject(restartRequester.services)){
					$('#restart_info .content').append(
						'<p>Please select services you want to restart!</p>'
					);
					return false;
				}

				restartRequester.requestUrl = <?php echo json_encode(url('whm/account/restart')); ?>;
				restartRequester.beforeRequest = function(id_account, doTimes){
					$('#restart_btn').button('loading');
					$('#restart_info .content').append(
						'<p>'+$('#id_account option[value="'+id_account+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Accounts)</p>'
					);
				};
				restartRequester.afterDone = function(){
					$('#restart_btn').button('reset');
				}
				restartRequester.requestCallback = function(id_account, serverReturn, doTimes){
					if(serverReturn.result){
						$.each(serverReturn.restart, function(service, detail){
							if(detail.result == 1){
								var result = '<p class="text-success accordion-toggle" data-toggle="collapse" data-target="#'+service+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+service+' - Success</b> Show/Hide Raw Output</p>';
							}else{
								var result = '<p class="text-error accordion-toggle" data-toggle="collapse" data-target="#'+service+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+service+' - Failed</b> Show/Hide Raw Output</p>';
							}
							$('#restart_info .content').append(result);
							$('#restart_info .content').append('<p id="'+service+serverReturn.time+'" class="muted collapse out">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>'+($.isEmptyObject(detail.rawout)? detail.servicename : detail.rawout)+'</b></p>');
						});
					}else{
						$.each(serverReturn.errors, function(attrName, error){
							$(error).each(function(k, e){
								$('#restart_info .content').append(
									'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#id_account option[value="'+id_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</b></p>'
								);
							});
						});
					}
				};
				restartRequester.do();
			},
			'showTime': 0
		});
		
	});
</script>